/*****************************************************************
*
*	Article:
*		How to register a system-wide hotkey in a console application
*
*	Authors:
*		Miles Chen (stainboyx@hotmail.com)
*
*	Reference:
*		http://msdn.microsoft.com/en-us/library/ms646309(VS.85).aspx
*
*	Build and Run:
*		cl /EHsc /Fosimple cpp-register-system-hotkey-console.cpp & simple
*
*****************************************************************/
#ifndef UNICODE
#define UNICODE
#endif

#include <Windows.h>
#pragma comment(lib, "Kernel32.lib")
#pragma comment(lib, "User32.lib")

#include <iostream>

using namespace std;

#define WM_HOTKEY_START	WM_USER + 100
#define WM_HOTKEY_END	WM_USER + 101

int main()
{
	if (RegisterHotKey(
		NULL,
		WM_HOTKEY_START,
		MOD_SHIFT | MOD_CONTROL,
		'S') &&
		RegisterHotKey(
		NULL,
		WM_HOTKEY_END,
		MOD_SHIFT | MOD_CONTROL,
		'Q'))
	{
		cout << "Hotkey 'CTL+SHT+S' and 'CTL+SHT+Q' registered" << endl;
	}
	else
	{
		cout << "Failed to register hotkey 'CTL+SHT+S' and 'CTL+SHT+Q'" << endl;
		return -1;
	}
 
	MSG msg = {0};
	while (GetMessageW(&msg, NULL, 0, 0) != 0)
	{
		if (msg.message == WM_HOTKEY)
		{
			if(msg.wParam ==  WM_HOTKEY_START)
			{
				cout << "Hotkey 'CTL+SHT+S' was pressed, I'm doing something..." << endl;
			}
			else if(msg.wParam ==  WM_HOTKEY_END)
			{
				cout << "Hotkey 'CTL+SHT+Q' was pressed, quit" << endl;
				break;
			}
		}
	} 


	UnregisterHotKey(NULL, WM_HOTKEY_START);
	UnregisterHotKey(NULL, WM_HOTKEY_END);
	return 0;
}